# Maintainer: Marcin Sielski <marcin.sielski(at)gmail.com>

_realname=paho.mqtt.c
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.3.0
pkgrel=1
pkgdesc="Eclipse Paho MQTT C client (mingw-w64)"
arch=('any')
url='https://eclipse.org/paho/clients/c/'
license=('EPL/EDL')
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake")
options=('strip' 'staticlibs')
source=("${_realname}-${pkgver}.tar.gz"::"https://github.com/eclipse/${_realname}/archive/v${pkgver}.tar.gz"
        002-fix-build.patch)
sha256sums=('87cf846b02dde6328b84832287d8725d91f12f41366eecb4d59eeda1d6c7efdf'
            '016b8a1f7716bd2b916ac54afed349e4943b1c682870c9997aa155b8c7805e72')

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}
  patch -p1 -i ${srcdir}/002-fix-build.patch
}

build() {
  [[ -d "${srcdir}"/build-${CARCH} ]] && rm -rf "${srcdir}"/build-${CARCH}
  mkdir -p "${srcdir}"/build-${CARCH} && cd "${srcdir}"/build-${CARCH}

  CFLAGS+=" -D_WINDOWS"
  CXXFLAGS+=" -D_WINDOWS"
  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -G'MSYS Makefiles' \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      "${extra_config[@]}" \
      -DPAHO_BUILD_STATIC=ON \
      -DPAHO_WITH_SSL=OFF \
      -DPAHO_ENABLE_CPACK=OFF \
      ../${_realname}-${pkgver}

  make
}

check() {
  cd "${srcdir}"/build-${CARCH}
}

package() {
  cd "${srcdir}"/build-${CARCH}
  make install DESTDIR="${pkgdir}"
  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/LICENSE ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
